package com.alibaba.citrus.cr.sales.plan.repository;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.common.exception.RepositoryException;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.FlowInstanceStartUpRequest;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.TaskSubmitRequest;
import com.alibaba.citrus.cr.sales.plan.dto.api.response.FlowInstanceStartUpResponse;
import com.alibaba.citrus.cr.sales.plan.converter.MeshAuditFlowRepositoryConverter;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.bcorder.model.dto.SalesPlanSubmitTaskRequest;
import com.epoch.app.bcorder.model.dto.SalesPlanSubmitTaskResponse;
import com.epoch.app.bcorder.model.dto.StartUpAuditFlowInstanceRequest;
import com.epoch.app.bcorder.model.dto.StartUpAuditFlowInstanceResponse;
import com.epoch.app.bcorder.service.SalesPlanService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 纪元审批流服务资源层实现
 * @author:suqing.tjf
 * @date:2021/9/23
 */
@Component
public class MeshAuditFlowRepositoryImpl implements MeshAuditFlowRepository {

    private final Log log = Log.getLogger(MeshAuditFlowRepositoryImpl.class);

    @Resource
    private SalesPlanService salesPlanService;

    /**
     * 启动审批引擎
     * @param request
     * @return
     */
    @Override
    @RepositoryInvoker(printLog = true)
    public FlowInstanceStartUpResponse startDefaultProcess(FlowInstanceStartUpRequest request) {
        StartUpAuditFlowInstanceRequest instanceRequest = MeshAuditFlowRepositoryConverter.convertStartUpAuditFlowInstanceRequest(request);
        StartUpAuditFlowInstanceResponse response = null;
        try {
            response =  salesPlanService.startUpAuditFlowInstance(instanceRequest);
        } catch (Exception e) {
            throw new RepositoryException(ErrorMessage.code("OTS-02-003-01-10-001"));
        }
        return MeshAuditFlowRepositoryConverter.convertFlowInstanceStartUpResponse(response);
    }

    /**
     * 提交审批任务
     *
     * @param request
     * @return
     */
    @Override
    @RepositoryInvoker(printLog = true)
    public String submitUserTask(TaskSubmitRequest request) {
        SalesPlanSubmitTaskRequest taskRequest = MeshAuditFlowRepositoryConverter.convertSalesPlanSubmitTaskRequest(request);
        log.error("MeshAuditFlowRepositoryImpl.submitUserTask.taskRequest={}", JSONObject.toJSONString(taskRequest));
        SalesPlanSubmitTaskResponse result = null;
        try {
            result = salesPlanService.salesPlanSubmitTask(taskRequest);
            log.info("任务id:{}执行结果:{}", request.getTaskId(), JSONObject.toJSONString(result));
            if (null != result && !result.getSuccess()) {
                return result.getMessage();
            }
        } catch (Exception e) {
            log.error("任务id:{}执行失败", request.getTaskId(), e);
            throw new RepositoryException(ErrorMessage.code("OTS-02-003-01-10-002"));
        }
        return "";
    }
}
